package com.guoheng.hazard.sys.service.impl.hazard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.common.enums.ErrorCodeEnum;
import com.guoheng.hazard.common.util.StringUtils;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicInfoMapper;
import com.guoheng.hazard.sys.mapper.entpr.ProductionPlaceMapper;
import com.guoheng.hazard.sys.mapper.entpr.StorageRoomMapper;
import com.guoheng.hazard.sys.mapper.entpr.StorageTankMapper;
import com.guoheng.hazard.sys.mapper.hazard.MajorHazardInfoMapper;
import com.guoheng.hazard.sys.mapper.hazard.MonitorCameraMapper;
import com.guoheng.hazard.sys.model.dto.entpr.EntprScopeDTO;
import com.guoheng.hazard.sys.model.dto.hazard.MonitorCameraInsertDTO;
import com.guoheng.hazard.sys.model.dto.hazard.MonitorCameraPageDTO;
import com.guoheng.hazard.sys.model.dto.hazard.MonitorCameraUpdateDTO;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicInfo;
import com.guoheng.hazard.sys.model.entity.entpr.ProductionPlace;
import com.guoheng.hazard.sys.model.entity.entpr.StorageRoom;
import com.guoheng.hazard.sys.model.entity.entpr.StorageTank;
import com.guoheng.hazard.sys.model.entity.hazard.MajorHazardInfo;
import com.guoheng.hazard.sys.model.entity.hazard.MonitorCamera;
import com.guoheng.hazard.sys.service.hazard.MonitorCameraService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述:　MonitorCameraServiceImpl
 *
 * @author Eternal
 * @date 2019-11-20 17:02
 */
@Service
public class MonitorCameraServiceImpl implements MonitorCameraService {

    @Resource
    MonitorCameraMapper monitorCameraMapper;

    @Resource
    MajorHazardInfoMapper majorHazardInfoMapper;

    @Resource
    StorageTankMapper storageTankMapper;

    @Resource
    StorageRoomMapper storageRoomMapper;

    @Resource
    ProductionPlaceMapper productionPlaceMapper;

    @Resource
    EntprBasicInfoMapper entprBasicInfoMapper;

    @Override
    public Result getMonitorCameras(Long entprId, String entprName, String cameraNo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MonitorCameraPageDTO> monitorCameras = monitorCameraMapper.getMonitorCameras(entprId, entprName, cameraNo);
        for (MonitorCameraPageDTO monitorCamera : monitorCameras) {
            Long majorHazardId = monitorCamera.getMajorHazardId();
            if(majorHazardId != null){
                // 查询重大危险源名称
                MajorHazardInfo majorHazardInfo = majorHazardInfoMapper.selectByPrimaryKey(majorHazardId);
                if(majorHazardInfo != null){
                    monitorCamera.setMajorHazardName(majorHazardInfo.getMajorHazardName());
                }
            }
            getPartName(monitorCamera);

        }
        PageInfo<MonitorCameraPageDTO> monitorCameraPageInfo = new PageInfo<>(monitorCameras);
        return Result.success(monitorCameraPageInfo);
    }

    @Override
    public Result updateMonitorCamera(MonitorCameraUpdateDTO majorMonitorCameraUpdateDTO) {
        MonitorCamera monitorCamera = new MonitorCamera();
        BeanUtils.copyProperties(majorMonitorCameraUpdateDTO, monitorCamera);
        // 判断摄像头是否修改
        MonitorCamera monitorCamera1 = monitorCameraMapper.selectByPrimaryKey(monitorCamera.getId());
        // 修改了
        if(!monitorCamera1.getCameraNo().equals(monitorCamera.getCameraNo())){
            // 摄像头编号不能重复
            List<MonitorCameraPageDTO> monitorCameras = monitorCameraMapper.getMonitorCameras(monitorCamera.getEntprId(), null, null);
            for (MonitorCameraPageDTO camera : monitorCameras) {
                // 摄像头编号重复
                if(camera.getCameraNo().equals(monitorCamera.getCameraNo())){
                    return Result.failure(ErrorCodeEnum.SYS_ERR_CAMERA_NO_REPEAT);
                }
            }
        }
        monitorCameraMapper.updateByPrimaryKeySelective(monitorCamera);
        return Result.success();
    }

    @Override
    public Result insertMonitorCamera(MonitorCameraInsertDTO monitorCameraInsertDTO) {
        MonitorCamera monitorCamera = new MonitorCamera();
        BeanUtils.copyProperties(monitorCameraInsertDTO, monitorCamera);
        // 摄像头编号不能重复
        List<MonitorCameraPageDTO> monitorCameras = monitorCameraMapper.getMonitorCameras(monitorCamera.getEntprId(), null, null);
        for (MonitorCameraPageDTO camera : monitorCameras) {
            // 摄像头编号重复
            if(camera.getCameraNo().equals(monitorCamera.getCameraNo())){
                return Result.failure(ErrorCodeEnum.SYS_ERR_CAMERA_NO_REPEAT);
            }
        }
        monitorCameraMapper.insertSelective(monitorCamera);
        return Result.success();
    }

    @Override
    public Result deleteMonitorCamera(Long id) {
        monitorCameraMapper.deleteByPrimaryKey(id);
        return Result.success();
    }

    @Override
    public Result getMonitorCamera(Long id) {
        MonitorCameraPageDTO monitorCameraPageDTO = new MonitorCameraPageDTO();
        MonitorCamera monitorCamera = monitorCameraMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(monitorCamera,monitorCameraPageDTO);
        Long majorHazardId = monitorCamera.getMajorHazardId();
        // 查询重大危险源名称
        if(majorHazardId != null){
            MajorHazardInfo majorHazardInfo = majorHazardInfoMapper.selectByPrimaryKey(majorHazardId);
            monitorCameraPageDTO.setMajorHazardName(majorHazardInfo.getMajorHazardName());
        }
        getPartName(monitorCameraPageDTO);
        Long entprId = monitorCamera.getEntprId();
        EntprBasicInfo entprBasicInfo = entprBasicInfoMapper.getEntprBasicInfoById(entprId);
        String entprScope = entprBasicInfo.getEntprScope();
        if (StringUtils.isNotEmpty(entprScope)) {
            entprScope = "{\"data\":" + entprScope + "}";
            JSONObject jsonObject = JSON.parseObject(entprScope);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            List<EntprScopeDTO> entprScopeDTOS = jsonArray.toJavaList(EntprScopeDTO.class);
            monitorCameraPageDTO.setEntprScope(entprScopeDTOS);
        }else{
            monitorCameraPageDTO.setEntprScope(new ArrayList<>());
        }
        return Result.success(monitorCameraPageDTO);
    }

    /**
     * 置入组成部分名称
     *
     * @param monitorCameraPageDTO 摄像头
     */
    private void getPartName(MonitorCameraPageDTO monitorCameraPageDTO){
        Integer partType = monitorCameraPageDTO.getPartType();
        Long partId = monitorCameraPageDTO.getPartId();
        if(partId != null){
            switch (partType) {
                case 1:
                    // 储罐
                    StorageTank tank = storageTankMapper.getEntprTankById(partId);
                    monitorCameraPageDTO.setPartName(tank.getTankName());
                    monitorCameraPageDTO.setPartNo(tank.getTankNo());
                    break;
                case 2:
                    // 库区
                    StorageRoom room = storageRoomMapper.getEntprRoomById(partId);
                    monitorCameraPageDTO.setPartName(room.getRoomName());
                    monitorCameraPageDTO.setPartNo(room.getRoomNo());
                    break;
                case 3:
                    // 生产场所
                    ProductionPlace place = productionPlaceMapper.getPlaceById(partId);
                    monitorCameraPageDTO.setPartName(place.getPlaceName());
                    monitorCameraPageDTO.setPartNo(place.getPlaceNo());
                    break;
                default:
                    break;
            }
        }
    }
}
